package com.yserp.hgv.service.impl;
import com.yserp.hgv.service.VHgorderServiceI;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import com.yserp.hgv.entity.VHgorderEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.io.Serializable;
import org.jeecgframework.core.util.ApplicationContextUtil;
import org.jeecgframework.core.util.MyClassLoader;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.web.cgform.enhance.CgformEnhanceJavaInter;

@Service("vHgorderService")
@Transactional
public class VHgorderServiceImpl extends CommonServiceImpl implements VHgorderServiceI {

	
 	public void delete(VHgorderEntity entity) throws Exception{
 		super.delete(entity);
 		//执行删除操作增强业务
		this.doDelBus(entity);
 	}
 	
 	public Serializable save(VHgorderEntity entity) throws Exception{
 		Serializable t = super.save(entity);
 		//执行新增操作增强业务
 		this.doAddBus(entity);
 		return t;
 	}
 	
 	public void saveOrUpdate(VHgorderEntity entity) throws Exception{
 		super.saveOrUpdate(entity);
 		//执行更新操作增强业务
 		this.doUpdateBus(entity);
 	}
 	
 	/**
	 * 新增操作增强业务
	 * @param t
	 * @return
	 */
	private void doAddBus(VHgorderEntity t) throws Exception{
		//-----------------sql增强 start----------------------------
	 	//-----------------sql增强 end------------------------------
	 	
	 	//-----------------java增强 start---------------------------
	 	//-----------------java增强 end-----------------------------
 	}
 	/**
	 * 更新操作增强业务
	 * @param t
	 * @return
	 */
	private void doUpdateBus(VHgorderEntity t) throws Exception{
		//-----------------sql增强 start----------------------------
	 	//-----------------sql增强 end------------------------------
	 	
	 	//-----------------java增强 start---------------------------
	 	//-----------------java增强 end-----------------------------
 	}
 	/**
	 * 删除操作增强业务
	 * @param id
	 * @return
	 */
	private void doDelBus(VHgorderEntity t) throws Exception{
	    //-----------------sql增强 start----------------------------
	 	//-----------------sql增强 end------------------------------
	 	
	 	//-----------------java增强 start---------------------------
	 	//-----------------java增强 end-----------------------------
 	}
 	
 	private Map<String,Object> populationMap(VHgorderEntity t){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("docnum", t.getDocnum());
		map.put("id", t.getId());
		map.put("company", t.getCompany());
		map.put("bm", t.getBm());
		map.put("create_by", t.getCreateBy());
		map.put("create_name", t.getCreateName());
		map.put("create_date", t.getCreateDate());
		map.put("update_date", t.getUpdateDate());
		map.put("cardcode", t.getCardcode());
		map.put("cardname", t.getCardname());
		map.put("invcode", t.getInvcode());
		map.put("tdcode", t.getTdcode());
		map.put("plandtime", t.getPlandtime());
		map.put("duedate", t.getDuedate());
		map.put("cyfy", t.getCyfy());
		map.put("gdfy", t.getGdfy());
		map.put("hzfy", t.getHzfy());
		map.put("total", t.getTotal());
		map.put("docstatus", t.getDocstatus());
		map.put("hscode", t.getHscode());
		map.put("hsname", t.getHsname());
		map.put("etdate", t.getEtdate());
		map.put("mtou", t.getMtou());
		map.put("pod", t.getPod());
		map.put("pol", t.getPol());
		map.put("ifcustoms", t.getIfcustoms());
		map.put("ei_type", t.getEiType());
		map.put("shiping", t.getShiping());
		map.put("tran_type", t.getTranType());
		map.put("fxdonum", t.getFxdonum());
		map.put("doctotal", t.getDoctotal());
		return map;
	}
 	
 	/**
	 * 替换sql中的变量
	 * @param sql
	 * @param t
	 * @return
	 */
 	public String replaceVal(String sql,VHgorderEntity t){
 		sql  = sql.replace("#{docnum}",String.valueOf(t.getDocnum()));
 		sql  = sql.replace("#{id}",String.valueOf(t.getId()));
 		sql  = sql.replace("#{company}",String.valueOf(t.getCompany()));
 		sql  = sql.replace("#{bm}",String.valueOf(t.getBm()));
 		sql  = sql.replace("#{create_by}",String.valueOf(t.getCreateBy()));
 		sql  = sql.replace("#{create_name}",String.valueOf(t.getCreateName()));
 		sql  = sql.replace("#{create_date}",String.valueOf(t.getCreateDate()));
 		sql  = sql.replace("#{update_date}",String.valueOf(t.getUpdateDate()));
 		sql  = sql.replace("#{cardcode}",String.valueOf(t.getCardcode()));
 		sql  = sql.replace("#{cardname}",String.valueOf(t.getCardname()));
 		sql  = sql.replace("#{invcode}",String.valueOf(t.getInvcode()));
 		sql  = sql.replace("#{tdcode}",String.valueOf(t.getTdcode()));
 		sql  = sql.replace("#{plandtime}",String.valueOf(t.getPlandtime()));
 		sql  = sql.replace("#{duedate}",String.valueOf(t.getDuedate()));
 		sql  = sql.replace("#{cyfy}",String.valueOf(t.getCyfy()));
 		sql  = sql.replace("#{gdfy}",String.valueOf(t.getGdfy()));
 		sql  = sql.replace("#{hzfy}",String.valueOf(t.getHzfy()));
 		sql  = sql.replace("#{total}",String.valueOf(t.getTotal()));
 		sql  = sql.replace("#{docstatus}",String.valueOf(t.getDocstatus()));
 		sql  = sql.replace("#{hscode}",String.valueOf(t.getHscode()));
 		sql  = sql.replace("#{hsname}",String.valueOf(t.getHsname()));
 		sql  = sql.replace("#{etdate}",String.valueOf(t.getEtdate()));
 		sql  = sql.replace("#{mtou}",String.valueOf(t.getMtou()));
 		sql  = sql.replace("#{pod}",String.valueOf(t.getPod()));
 		sql  = sql.replace("#{pol}",String.valueOf(t.getPol()));
 		sql  = sql.replace("#{ifcustoms}",String.valueOf(t.getIfcustoms()));
 		sql  = sql.replace("#{ei_type}",String.valueOf(t.getEiType()));
 		sql  = sql.replace("#{shiping}",String.valueOf(t.getShiping()));
 		sql  = sql.replace("#{tran_type}",String.valueOf(t.getTranType()));
 		sql  = sql.replace("#{fxdonum}",String.valueOf(t.getFxdonum()));
 		sql  = sql.replace("#{doctotal}",String.valueOf(t.getDoctotal()));
 		sql  = sql.replace("#{UUID}",UUID.randomUUID().toString());
 		return sql;
 	}
 	
 	/**
	 * 执行JAVA增强
	 */
 	private void executeJavaExtend(String cgJavaType,String cgJavaValue,Map<String,Object> data) throws Exception {
 		if(StringUtil.isNotEmpty(cgJavaValue)){
			Object obj = null;
			try {
				if("class".equals(cgJavaType)){
					//因新增时已经校验了实例化是否可以成功，所以这块就不需要再做一次判断
					obj = MyClassLoader.getClassByScn(cgJavaValue).newInstance();
				}else if("spring".equals(cgJavaType)){
					obj = ApplicationContextUtil.getContext().getBean(cgJavaValue);
				}
				if(obj instanceof CgformEnhanceJavaInter){
					CgformEnhanceJavaInter javaInter = (CgformEnhanceJavaInter) obj;
					javaInter.execute("v_hgorder",data);
				}
			} catch (Exception e) {
				e.printStackTrace();
				throw new Exception("执行JAVA增强出现异常！");
			} 
		}
 	}
}